|
1
|
|
|
var GedcomX = require('../'), |
|
2
|
|
|
utils = require('../utils'); |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* A relationship. |
|
6
|
|
|
* |
|
7
|
|
|
* @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#relationship|GEDCOM X JSON Spec} |
|
8
|
|
|
* |
|
9
|
|
|
* @class |
|
10
|
|
|
* @extends Subject |
|
11
|
|
|
* @param {Object} [json] |
|
|
|
|
|
|
12
|
|
|
*/ |
|
13
|
|
|
var Relationship = function(json){ |
|
14
|
|
|
|
|
15
|
|
|
// Protect against forgetting the new keyword when calling the constructor |
|
16
|
|
|
if(!(this instanceof Relationship)){ |
|
17
|
|
|
return new Relationship(json); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// If the given object is already an instance then just return it. DON'T copy it. |
|
21
|
|
|
if(Relationship.isInstance(json)){ |
|
22
|
|
|
return json; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
this.init(json); |
|
|
|
|
|
|
26
|
|
|
}; |
|
27
|
|
|
|
|
28
|
|
|
Relationship.prototype = Object.create(GedcomX.Subject.prototype); |
|
29
|
|
|
|
|
30
|
|
|
Relationship._gedxClass = Relationship.prototype._gedxClass = 'GedcomX.Relationship'; |
|
31
|
|
|
|
|
32
|
|
|
Relationship.jsonProps = [ |
|
33
|
|
|
'type', |
|
34
|
|
|
'person1', |
|
35
|
|
|
'person2', |
|
36
|
|
|
'facts' |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check whether the given object is an instance of this class. |
|
41
|
|
|
* |
|
42
|
|
|
* @param {Object} obj |
|
43
|
|
|
* @returns {Boolean} |
|
44
|
|
|
*/ |
|
45
|
|
|
Relationship.isInstance = function(obj){ |
|
46
|
|
|
return utils.isInstance(obj, this._gedxClass); |
|
47
|
|
|
}; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Initialize from JSON |
|
51
|
|
|
* |
|
52
|
|
|
* @param {Object} |
|
|
|
|
|
|
53
|
|
|
* @return {Relationship} this |
|
54
|
|
|
*/ |
|
55
|
|
|
Relationship.prototype.init = function(json){ |
|
56
|
|
|
|
|
57
|
|
|
GedcomX.Subject.prototype.init.call(this, json); |
|
58
|
|
|
|
|
59
|
|
|
if(json){ |
|
60
|
|
|
this.setType(json.type); |
|
61
|
|
|
this.setPerson1(json.person1); |
|
62
|
|
|
this.setPerson2(json.person2); |
|
63
|
|
|
this.setFacts(json.facts); |
|
64
|
|
|
} |
|
65
|
|
|
return this; |
|
66
|
|
|
}; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get the relationship type |
|
70
|
|
|
* |
|
71
|
|
|
* @returns {String} type |
|
72
|
|
|
*/ |
|
73
|
|
|
Relationship.prototype.getType = function(){ |
|
74
|
|
|
return this.type; |
|
75
|
|
|
}; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set the relationship type |
|
79
|
|
|
* |
|
80
|
|
|
* @param {String} type |
|
81
|
|
|
* @returns {Relationship} This instance |
|
82
|
|
|
*/ |
|
83
|
|
|
Relationship.prototype.setType = function(type){ |
|
84
|
|
|
this.type = type; |
|
85
|
|
|
return this; |
|
86
|
|
|
}; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get person1 reference |
|
90
|
|
|
* |
|
91
|
|
|
* @returns {ResourceReference} |
|
92
|
|
|
*/ |
|
93
|
|
|
Relationship.prototype.getPerson1 = function(){ |
|
94
|
|
|
return this.person1; |
|
95
|
|
|
}; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set the person1 reference |
|
99
|
|
|
* |
|
100
|
|
|
* @param {ResourceReference} person1 |
|
101
|
|
|
* @returns {Relationship} This instance |
|
102
|
|
|
*/ |
|
103
|
|
|
Relationship.prototype.setPerson1 = function(person1){ |
|
104
|
|
|
if(person1){ |
|
105
|
|
|
this.person1 = GedcomX.ResourceReference(person1); |
|
106
|
|
|
} |
|
107
|
|
|
return this; |
|
108
|
|
|
}; |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get person2 reference |
|
112
|
|
|
* |
|
113
|
|
|
* @returns {ResourceReference} |
|
114
|
|
|
*/ |
|
115
|
|
|
Relationship.prototype.getPerson2 = function(){ |
|
116
|
|
|
return this.person2; |
|
117
|
|
|
}; |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Set the person1 reference |
|
121
|
|
|
* |
|
122
|
|
|
* @param {ResourceReference} person2 |
|
123
|
|
|
* @returns {Relationship} This instance |
|
124
|
|
|
*/ |
|
125
|
|
|
Relationship.prototype.setPerson2 = function(person2){ |
|
126
|
|
|
if(person2){ |
|
127
|
|
|
this.person2 = GedcomX.ResourceReference(person2); |
|
128
|
|
|
} |
|
129
|
|
|
return this; |
|
130
|
|
|
}; |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Calculate whether a relationship involves a person |
|
134
|
|
|
* |
|
135
|
|
|
* @param {Person|String} person Person object or person ID |
|
136
|
|
|
* @return {Boolean} |
|
137
|
|
|
*/ |
|
138
|
|
|
Relationship.prototype.involvesPerson = function(person){ |
|
139
|
|
|
if(person === undefined){ |
|
140
|
|
|
return false; |
|
141
|
|
|
} else if(this.person1 && this.person2){ |
|
142
|
|
|
return this.person1.matches(person) || this.person2.matches(person); |
|
143
|
|
|
} else if(this.person1){ |
|
144
|
|
|
return this.person1.matches(person); |
|
145
|
|
|
} else if(this.person2){ |
|
146
|
|
|
return this.person2.matches(person); |
|
147
|
|
|
} else { |
|
148
|
|
|
return false; |
|
149
|
|
|
} |
|
150
|
|
|
}; |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Given a person in this relationships, return the other person's resource reference. |
|
154
|
|
|
* |
|
155
|
|
|
* @param {Person|String} person Person object or person ID |
|
156
|
|
|
* @return {ResourceReference} |
|
157
|
|
|
*/ |
|
158
|
|
|
Relationship.prototype.getOtherPerson = function(person){ |
|
159
|
|
|
return this.person1 && this.person1.matches(person) ? this.person2 : this.person1; |
|
160
|
|
|
}; |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Get the facts |
|
164
|
|
|
* |
|
165
|
|
|
* @return {Fact[]} |
|
166
|
|
|
*/ |
|
167
|
|
|
Relationship.prototype.getFacts = function(){ |
|
168
|
|
|
return this.facts || []; |
|
169
|
|
|
}; |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Set the facts |
|
173
|
|
|
* |
|
174
|
|
|
* @param {Fact[]|Object[]} facts |
|
175
|
|
|
* @returns {Relationship} This instance |
|
176
|
|
|
*/ |
|
177
|
|
|
Relationship.prototype.setFacts = function(facts){ |
|
178
|
|
|
return this._setArray(facts, 'facts', 'addFact'); |
|
179
|
|
|
}; |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Add a fact |
|
183
|
|
|
* |
|
184
|
|
|
* @param {Fact|Object} fact |
|
185
|
|
|
* @returns {Relationship} This instance |
|
186
|
|
|
*/ |
|
187
|
|
|
Relationship.prototype.addFact = function(fact){ |
|
188
|
|
|
return this._arrayPush(fact, 'facts', GedcomX.Fact); |
|
189
|
|
|
}; |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Export the object as JSON |
|
193
|
|
|
* |
|
194
|
|
|
* @return {Object} JSON object |
|
195
|
|
|
*/ |
|
196
|
|
|
Relationship.prototype.toJSON = function(){ |
|
197
|
|
|
return this._toJSON(GedcomX.Subject, Relationship.jsonProps); |
|
198
|
|
|
}; |
|
199
|
|
|
|
|
200
|
|
|
module.exports = Relationship; |